package cn.sc.summer.construction.sysimpl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fasterxml.jackson.core.type.TypeReference;
import com.rabbitmq.client.Channel;
import cn.sc.summer.constant.enums.StatusEnum;
import cn.sc.summer.constant.rabbit.RabbitConstant;
import cn.sc.summer.daoapi.dao.SmsLogMapper;
import cn.sc.summer.mybatis.annotation.dynamicdatasource.DataSource;
import cn.sc.summer.mybatis.enums.DataSourceEnum;
import cn.sc.summer.rabbitmq.enums.RabbitErrorMessageTypeEnum;
import cn.sc.summer.rabbitmq.po.RabbitMessage;
import cn.sc.summer.rabbitmq.service.RabbitConsumerService;
import cn.sc.summer.rabbitmq.util.RabbitUtil;
import cn.sc.summer.constant.sms.SmsLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 类名：短信发送日志记录消费者
 *
 * @author a-xin
 * @date 2023/10/17 14:50
 */
@Slf4j
@Service
public class SmsServiceImpl extends RabbitConsumerService {

    @Resource
    private SmsLogMapper smsLogMapper;

    /**
     * 接口: 监听短信发布消息
     *
     * @param jsonStr 日志信息
     * @param message rabbit消息提
     * @param channel rabbit信道
     * @author a-xin
     * @date 2023/9/23 15:48
     */
    @RabbitListener(queues = RabbitConstant.SMS_RECORD_QUEUE)
    @DataSource(DataSourceEnum.POST_GRE_SQL)
    public void consumer(String jsonStr, Message message, Channel channel) {
        try {
            RabbitMessage<JSONObject> jsonObjectRabbitMessage = RabbitUtil.checkMessage(jsonStr, new TypeReference<JSONObject>() {});
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            if (jsonObjectRabbitMessage.getFlag()) {
                JSONObject jsonObject = jsonObjectRabbitMessage.getMessage();
                SmsLog smsLog = new SmsLog();
                smsLog.setId(IdWorker.getIdStr());
                smsLog.setPhone(jsonObject.getString("phone"));
                smsLog.setIpAddress(jsonObject.getString("ipAddress"));
                smsLog.setMessage(jsonObject.getString("template"));
                smsLog.setParams(jsonObject.getString("params"));
                smsLog.setCreateTime(new Date());
                smsLog.setDel(StatusEnum.ENABLE_OR_NOT_DELETED.getType());
                smsLogMapper.insert(smsLog);
            } else {
                log.error("==> 接收到重复消费消息：Queue: {}, Exchange : {}, RoutingKey : {}, Message : {}",
                        RabbitConstant.SMS_RECORD_QUEUE,
                        RabbitConstant.SMS_RECORD_EXCHANGE,
                        RabbitConstant.SMS_RECORD_ROUTING_KEY,
                        jsonStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
            rabbitErrorService.exchangeMessage(e.getMessage(), jsonStr, RabbitErrorMessageTypeEnum.SMS_WRITE);
        }
    }

}
